<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
	// 首页
	public function Verify(){
		ob_clean();
	    $Verify = new \Think\Verify();
		$Verify->fontSize = 30;
		$Verify->length   = 4;
		$Verify->codeSet = '0123456789'; 
		$Verify->useNoise = false;
		$Verify->entry();
	} 
    public function login(){

		$this->display();
    }
	public function checkLogin(){
		$adminname=$_POST['adminname'];

		$password=$_POST['password'];
		if(empty($_POST['adminname'])) {

			    echo "<script>
			            alert('账号为空');
			            history.go(-1);
			          </script>";
			exit();
		}
		elseif (empty($_POST['password'])){

			    echo "<script>
			            alert('密码为空');
			            history.go(-1);
			          </script>";
		}
		$m=M("admin");
		$db=M("");
		$sql="select * from in_admin where adminname='{$adminname}'";
		$arr=$db->query($sql);
		/*echo md5('zqhf2016');
		dump($arr);
		//if($this->check_verify($code)){
		exit;*/
				if($arr!=""){
					$checkpwd=$m->field("password")->where("adminname='{$adminname}'")->find();
					if($checkpwd['password']==md5($password)){
					$_SESSION["adminname"]=$adminname;
					$_SESSION['password']=$password;

					    echo "<script>
					            location.href='/Admin/Index/index';//客户端跳转
					          </script>";
					}
					else{

			    echo "<script>
			            alert('密码不正');
			            history.go(-1);
			          </script>";
					}
					
				}else{
					echo "<script>
			            alert('该账号不存在');
			            history.go(-1);
			          </script>";
				}
			
			/*}
			else{
			
				$this->error("验证码不正确");

			}*/
		
	
	}
	public function editpwd(){
		$admin=$_SESSION["adminname"];
		$password=$_SESSION['password'];
		$this->assign('password',$password);
		$db=M("");
		$act=$_POST['act'];
		if($act!=""){
			unset($_POST['act']);
			$oldpwd=$_POST['oldpwd'];
			$newpassword=$_POST['newpassword'];
			$repassword=$_POST['repassword'];
			if($oldpwd==$password){
				if($newpassword==$repassword){
					$pwd=md5($newpassword);
					$sql="update in_admin set password='{$pwd}' where adminname='{$admin}'";
					if($db->execute($sql)){
						$this->success("修改成功");
					}
				}else{

					    echo "<script>
					            alert(俩次新密码输入不同);
					          </script>";
					// $this->error("俩次新密码输入不同");
				}
			}else{
				  			echo "<script>
					            alert(原密码输入不正确);
					          </script>";
			}
			
		}else{
			$this->display();
		}
		
	}
	public function exits(){
		if(isset($_SESSION["adminname"])) {
			unset($_SESSION);
			session_destroy();
			echo "<script>
			alert('你已成功退出');
            location.href='/Admin/Login/login';//客户端跳转
          </script>";
		}else {
			$this->error('已经登录！');
		}
	}

}
